package viewbuilder.java;

import static viewbuilder.java.HtmlAttribute.attrs;
import static viewbuilder.java.HtmlAttribute.attr;

import java.util.List;

/**
 *
 */
public class JavaTable {
    Customer customer;
    List<CustomerUser> users;
    HtmlBuilder builder;

    public JavaTable( Customer customer, List<CustomerUser> users ) {
        this.customer = customer;
        this.users = users;
        builder = HtmlBuilderFactory.createBuilder();
    }

    public String render() {
        HtmlNode table = builder.table(
            tableHead(),
            builder.tbody(
                userRows()
            )
        );

        return table.render();
    }

    private HtmlNode[] userRows() {
        HtmlNode[] userRows = new HtmlNode[users.size()];
        int i=0;
        for(CustomerUser user: users) {
            userRows[i++] = builder.tr(attrs(attr("id", user.id.toString()), attr("class", "makeValid")),
                builder.td(attrs(attr("class", "status sortable-text")),
                    builder.span(attrs(attr("class", "sortdatum")),
                        builder.text("true"))),
                builder.td(attrs(attr("class", "sortable-text sortField-username")),
                    builder.text(user.username)),
                builder.td(attrs(attr("class", "sortable-text sortField-firstName")),
                    builder.text(user.firstName)),
                builder.td(attrs(attr("class", "sortable-text sortField-lastName")),
                    builder.text(user.lastName)));
        }

        return userRows;
    }

    private HtmlNode tableHead() {
        return builder.thead(
            builder.tr(
                builder.th( builder.text("status")),
                builder.th( attrs(
                    attr( "id", "username" ),
                    attr( "scope", "col" ) ),
                    builder.text( "Username" ) ),
                builder.th( attrs(
                    attr( "id", "firstName" ),
                    attr( "scope", "col" ) ),
                    builder.text( "First name" ) ),
                builder.th( attrs(
                    attr( "id", "lastName" ),
                    attr( "scope", "col" ) ),
                    builder.text( "Last name" ) ),
                builder.th( attrs( attr( "class", "hideprint" ), attr( "id", "actions" ) ),
                    builder.text( "&nbsp;" ) )
            )
        );
    }
}

